<?php
    class BannchatController extends CpController{
        public function init(){
            $url = new Url();
            $this->breadcrumbs[] = array('name'=>'Quản trị Bann Chat','link'=>$url->createUrl("bannchat/index"),'class'=>'');
        }
        public function actionIndex(){
          
            $row_per_page = 20; 
            $this->breadcrumbs[] = array('name'=>'Danh sách Bann Chat','link'=>"#",'class'=>'active');
            $username = isset($_GET["username"]) ? trim($_GET["username"]):"";
            $bann = isset($_GET["bann"]) ? intval($_GET["bann"]):2;
            $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
            list($max_page,$count,$data) = ABannChat::getDataSearch($username,$bann,$page,$row_per_page);
            $this->render("index",
                array(
                    "data"=>$data,
                    "username"=>$username,
                    "max_page"=>$max_page,
                    "count"=>$count,
                    "bann"=>$bann,
                    "page"=>$page
                )
            );
        }
        public function actionAdd(){
            $this->breadcrumbs[] = array('name'=>'Thêm mới Blacklist Chat','link'=>"#",'class'=>'active');
            $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):"";
            $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):"";
            $this->render("create",
                array(
                    "from_date"=>$from_date
                    , "to_date"=>$to_date
                )
            );
        }
        public function actionAjaxQuickUpdate(){
            $id = isset($_POST["id"]) ? intval($_POST["id"]):0;   
            $bann_chat =0;
            $from_date=null;
            $to_date=null;
            if($id >0){
                $data_old = ABannChat::getDataById($id);
                $data = array(
                "bann_chat"=>array("value"=>$bann_chat,"type"=>1),
                "id"=>array("value"=>$id,"type"=>1),
                "from_date"=>array("value"=>$from_date,"type"=>1),
                "to_date"=>array("value"=>$to_date,"type"=>1),
                );
                $table = "c_user";
                $result = CommonDB::updateRow($table,$data,array("id"));
                if($result >0){
                    $act = 2;
                    $obj = 20;
                    $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',bann_chat: '.$data_old["bann_chat"].'} thành ';
                    $content .= ' {id: '.$id.',bann_chat: '.$bann_chat.'}';
                    $result = AAdminLog::insertAdminLog($act,$obj,$content);
                    echo 1;exit;
                }else{
                    echo "Chưa thay đổi dữ liệu";exit;
                }
            }
        }
        public function actionAjaxAddBannChat(){
            $arr_date=array();
            $id = isset($_POST["id"]) ? intval($_POST["id"]):0;   
            $from_date = isset($_POST["from_date"]) ?$_POST["from_date"]:"";
            $to_date = isset($_POST["to_date"]) ? $_POST["to_date"]:"";
            
            if($from_date=="" && $to_date=="" ){
                echo "Bạn chưa nhập thời gian Bann"; die;
            }
            /*Chuyển kiểu datetime sang kiểu int*/
            $arr_date = explode("-",$from_date);
            $from_date = mktime(0,0,0,$arr_date[1],$arr_date[0],$arr_date[2]);
            
            $arr_date = explode("-",$to_date);
            $to_date = mktime(23,59,59,$arr_date[1],$arr_date[0],$arr_date[2]);
            
            if($from_date>$to_date){
                echo "Thời gian nhập chưa chính xác"; die;
            }
            $bann_chat=1;
            if($id >0){
                $data_old = ABannChat::getDataById($id);
                $data = array(
                "bann_chat"=>array("value"=>$bann_chat,"type"=>1),
                "id"=>array("value"=>$id,"type"=>1),
                "from_date"=>array("value"=>$from_date,"type"=>1),
                "to_date"=>array("value"=>$to_date,"type"=>1),
                );
                $table = "c_user";
                $result = CommonDB::updateRow($table,$data,array("id"));
                if($result >0){
                    $act = 2;
                    $obj = 20;
                    $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',bann_chat: '.$data_old["bann_chat"].'} thành ';
                    $content .= ' {id: '.$id.',bann_chat: '.$bann_chat.'}';
                    $result = AAdminLog::insertAdminLog($act,$obj,$content);
                    echo 1;exit;
                }else{
                    echo "Chưa thay đổi dữ liệu";exit;
                }
            }
        }
    }
?>
